import PAGE from '../../utils/middleware/page'
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import store from '../../store/index'
const app = getApp()

Page(PAGE({
    behaviors: [storeBindingsBehavior],
    storeBindings: {
        store,
        fields: {
            needRefresh: 'vehicle.needRefresh',
            needNavigate: 'vehicle.needNavigate',
            needNavigatePage: 'vehicle.needNavigatePage'
        },
        actions: {
            updateNeedRefresh: 'vehicle.updateNeedRefresh',
            updateNeedNavigate: 'vehicle.updateNeedNavigate'
        },

    },
    data: {
        loginUserInfo: {},
        isLogin: app.globalData.isLogin,
        report: {},
        apiField: {
            module: 'common',
            api: 'HomeReport',
            data: 'report',
            search: {}
        }
    },
    async onLoad() {
        if (app.globalData.isLogin) {
            this.getLoginUserInfo()
            this.getHomeReport()
        }
    },
    onShow() {
        if (this.data.needRefresh && app.globalData.isLogin) {
            this.setData({
                isLogin: app.globalData.isLogin
            })
            this.updateNeedRefresh(false)
            this.getLoginUserInfo()
            this.getHomeReport()
        }
        if (this.data.needNavigate) {
            this.updateNeedNavigate(false)
            wx.$Fun.toPage({
                page: this.data.needNavigatePage
            })
        }
    },
    onPullDownRefresh() {
        if (app.globalData.isLogin) {
            this.getLoginUserInfo()
            this.getHomeReport()
        }
        wx.stopPullDownRefresh();
    },
    handleToPage(e) {
        const { page, login, params } = e.currentTarget.dataset
        if (!page) {
            wx.$Fun.showToast("敬请期待！")
            return
        }
        if (login && !app.globalData.isLogin) {
            wx.$Fun.showToast("请登录！")
            return
        }
        wx.$Fun.toPage({
            page: page,
            params
        })
    },
    getHomeReport() {
        this.handleFetchData({})
    },
    async getLoginUserInfo() {
        try {
            const res = await wx.$Api.member.GetLoginUser({ data: {}, hasLoading: false })
            this.setData({
                loginUserInfo: res.Data,
                hasUserInfo: true,
                isLogin: true
            })
        }
        catch (e) {
            this.setData({
                loginUserInfo: {},
                hasUserInfo: false,
                isLogin: false
            })
        }
    },
    handleCallPhone() {
        wx.makePhoneCall({
            phoneNumber: this.data.hotline,
        })
    },
    handlePreview(e) {
        var url = e.currentTarget.dataset.url
        if (url) {
            wx.previewImage({
                urls: [url],
            })
        }
    },
}))